<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="实验名称" prop="newExperimentName">
        <el-input
          v-model="queryParams.newExperimentName"
          placeholder="请输入实验名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="成绩" prop="newExperimentNumber">
        <el-input
          v-model="queryParams.newExperimentNumber"
          placeholder="请输入成绩"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
      <!-- </el-form-item> -->
      <el-form-item label="实验申请状态" prop="typeId">
        <el-input
          v-model="queryParams.typeId"
          placeholder="请输入实验申请状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提交时间" prop="newExperimentDate">
        <el-date-picker clearable
          v-model="queryParams.newExperimentDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择提交时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="专业" prop="newExperimentMajor">
        <el-input
          v-model="queryParams.newExperimentMajor"
          placeholder="请输入专业"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="上传文档" prop="newExperimentText">
        <el-input
          v-model="queryParams.newExperimentText"
          placeholder="请输入上传文档"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
      <!-- </el-form-item> -->
      <!-- <el-form-item label="是否自带动物;1：自带 2:不自带" prop="newExperimentByo">
        <el-input
          v-model="queryParams.newExperimentByo"
          placeholder="请输入是否自带动物;1：自带 2:不自带"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="自带动物申请ID" prop="byoId">
        <el-input
          v-model="queryParams.byoId"
          placeholder="请输入自带动物申请ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="动物申请ID" prop="animalApplicationId">
        <el-input
          v-model="queryParams.animalApplicationId"
          placeholder="请输入动物申请ID"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
      <!-- </el-form-item> -->
      <!-- <el-form-item label="实验器材申请ID" prop="equipmentApplicationId">
        <el-input
          v-model="queryParams.equipmentApplicationId"
          placeholder="请输入实验器材申请ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="备用列" prop="newExperimentBy1">
        <el-input
          v-model="queryParams.newExperimentBy1"
          placeholder="请输入备用列"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备用列" prop="newExperimentBy2">
        <el-input
          v-model="queryParams.newExperimentBy2"
          placeholder="请输入备用列"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备用列" prop="newExperimentBy3">
        <el-input
          v-model="queryParams.newExperimentBy3"
          placeholder="请输入备用列"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="选课ID" prop="curriculaVaribleId">
        <el-input
          v-model="queryParams.curriculaVaribleId"
          placeholder="请输入选课ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教师ID" prop="teacherId">
        <el-input
          v-model="queryParams.teacherId"
          placeholder="请输入教师ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="小组成员ID" prop="memberId">
        <el-input
          v-model="queryParams.memberId"
          placeholder="请输入小组成员ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="学生ID" prop="studentId">
        <el-input
          v-model="queryParams.studentId"
          placeholder="请输入学生ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['experiment:experiment:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['experiment:experiment:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['experiment:experiment:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['experiment:experiment:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="experimentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="实验ID" align="center" prop="newExperimentId" />
      <el-table-column label="实验名称" align="center" prop="newExperimentName" />
      <!-- <el-table-column label="成绩" align="center" prop="newExperimentNumber" /> -->
      <el-table-column label="实验申请状态" align="center" prop="typeId" />
      <el-table-column label="提交时间" align="center" prop="newExperimentDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.newExperimentDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="专业" align="center" prop="newExperimentMajor" />
      <el-table-column label="详细内容" align="center" prop="newExperimentContent" />
      <el-table-column label="上传文档" align="center" prop="newExperimentText" />
      <el-table-column label="是否自带动物" align="center" prop="newExperimentByo" >
        <template slot-scope="scope">
            <p v-if="scope.row.newExperimentByo=='1'">
            <el-button disabled
                       size="mini"
                       type="text"
                       icon="el-icon-check">自带</el-button>
          </p>
          <p v-if="scope.row.newExperimentByo=='2'">
            <el-button disabled
                       size="mini"
                       type="text"
                       icon="el-icon-close">不自带</el-button>
          </p>
        </template>
      </el-table-column>
      <!-- <el-table-column label="自带动物申请ID" align="center" prop="byoId" />
      <el-table-column label="动物申请ID" align="center" prop="animalApplicationId" />
      <el-table-column label="实验器材申请ID" align="center" prop="equipmentApplicationId" /> -->
      <!-- <el-table-column label="备用列" align="center" prop="newExperimentBy1" />
      <el-table-column label="备用列" align="center" prop="newExperimentBy2" />
      <el-table-column label="备用列" align="center" prop="newExperimentBy3" /> -->
      <!-- <el-table-column label="选课ID" align="center" prop="curriculaVaribleId" /> -->
      <!-- <el-table-column label="教师ID" align="center" prop="teacherId" /> -->
      <!-- <el-table-column label="小组成员ID" align="center" prop="memberId" /> -->
      <!-- <el-table-column label="学生ID" align="center" prop="studentId" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['experiment:experiment:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['experiment:experiment:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改申请新实验对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="实验名称" prop="newExperimentName">
          <el-input v-model="form.newExperimentName" placeholder="请输入实验名称" />
        </el-form-item>
        <!-- <el-form-item label="成绩" prop="newExperimentNumber">
          <el-input v-model="form.newExperimentNumber" placeholder="请输入成绩" />
        </el-form-item> -->
        <!-- <el-form-item label="实验申请状态" prop="typeId">
          <el-input v-model="form.typeId" placeholder="请输入实验申请状态" />
        </el-form-item> -->
        <el-form-item label="提交时间" prop="newExperimentDate">
          <el-date-picker clearable
            v-model="form.newExperimentDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择提交时间">
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="专业" prop="newExperimentMajor">
          <el-input v-model="form.newExperimentMajor" placeholder="请输入专业" />
        </el-form-item> -->
        <el-form-item label="详细内容">
          <editor v-model="form.newExperimentContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="上传文档" prop="newExperimentText">
          <el-input v-model="form.newExperimentText" placeholder="请输入上传文档" />
        </el-form-item>
        <el-form-item label="是否自带动物;1：自带 2:不自带" prop="newExperimentByo">
          <el-input v-model="form.newExperimentByo" placeholder="请输入是否自带动物;1：自带 2:不自带" />
        </el-form-item>
        <!-- <el-form-item label="自带动物申请ID" prop="byoId">
          <el-input v-model="form.byoId" placeholder="请输入自带动物申请ID" />
        </el-form-item>
        <el-form-item label="动物申请ID" prop="animalApplicationId">
          <el-input v-model="form.animalApplicationId" placeholder="请输入动物申请ID" />
        </el-form-item>
        <el-form-item label="实验器材申请ID" prop="equipmentApplicationId">
          <el-input v-model="form.equipmentApplicationId" placeholder="请输入实验器材申请ID" />
        </el-form-item> -->
        <!-- <el-form-item label="备用列" prop="newExperimentBy1">
          <el-input v-model="form.newExperimentBy1" placeholder="请输入备用列" />
        </el-form-item>
        <el-form-item label="备用列" prop="newExperimentBy2">
          <el-input v-model="form.newExperimentBy2" placeholder="请输入备用列" />
        </el-form-item>
        <el-form-item label="备用列" prop="newExperimentBy3">
          <el-input v-model="form.newExperimentBy3" placeholder="请输入备用列" />
        </el-form-item> -->
        <!-- <el-form-item label="选课ID" prop="curriculaVaribleId">
          <el-input v-model="form.curriculaVaribleId" placeholder="请输入选课ID" />
        </el-form-item>
        <el-form-item label="教师ID" prop="teacherId">
          <el-input v-model="form.teacherId" placeholder="请输入教师ID" />
        </el-form-item>
        <el-form-item label="小组成员ID" prop="memberId">
          <el-input v-model="form.memberId" placeholder="请输入小组成员ID" />
        </el-form-item> -->
        <el-form-item label="学生ID" prop="studentId">
          <el-input v-model="form.studentId" placeholder="请输入学生ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listExperiment, getExperiment, delExperiment, addExperiment, updateExperiment } from "@/api/experiment/experiment";

export default {
  name: "Experiment",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 申请新实验表格数据
      experimentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        newExperimentName: null,
        newExperimentNumber: null,
        typeId: null,
        newExperimentDate: null,
        newExperimentMajor: null,
        newExperimentContent: null,
        newExperimentText: null,
        newExperimentByo: null,
        byoId: null,
        animalApplicationId: null,
        equipmentApplicationId: null,
        newExperimentBy1: null,
        newExperimentBy2: null,
        newExperimentBy3: null,
        curriculaVaribleId: null,
        teacherId: null,
        memberId: null,
        studentId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询申请新实验列表 */
    getList() {
      this.loading = true;
      listExperiment(this.queryParams).then(response => {
        this.experimentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        newExperimentId: null,
        newExperimentName: null,
        newExperimentNumber: null,
        typeId: null,
        newExperimentDate: null,
        newExperimentMajor: null,
        newExperimentContent: null,
        newExperimentText: null,
        newExperimentByo: null,
        byoId: null,
        animalApplicationId: null,
        equipmentApplicationId: null,
        newExperimentBy1: null,
        newExperimentBy2: null,
        newExperimentBy3: null,
        curriculaVaribleId: null,
        teacherId: null,
        memberId: null,
        studentId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.newExperimentId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加申请新实验";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const newExperimentId = row.newExperimentId || this.ids
      getExperiment(newExperimentId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改申请新实验";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.newExperimentId != null) {
            updateExperiment(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addExperiment(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const newExperimentIds = row.newExperimentId || this.ids;
      this.$modal.confirm('是否确认删除申请新实验编号为"' + newExperimentIds + '"的数据项？').then(function() {
        return delExperiment(newExperimentIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('experiment/experiment/export', {
        ...this.queryParams
      }, `experiment_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
